<?php

// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台内容控制器
 * @author huajie <banhuajie@163.com>
 */
class GoodsController extends AdminController {

    protected function _initialize() {
        parent::_initialize();
    }

    public function index() {

        $this->display();
    }

    public function typelist() {
        $this->display();
    }

    public function speclist() {
        $this->display();
    }

    public function attrlist() {
        $this->display();
    }



    public function brandlist() {
        $title = I('title');
        if (!empty($title)) {
            $map['title'] = ['like', "%$title%"];
        }
        $sortfld = I('sortfld', "id", 'trim');
        $sortord = I('sortord', "desc", 'trim');
        $list = $this->lists('brand', $map, "`$sortfld` $sortord");
         $clist=M('goods_category')->where()->getField("id,name");
 
          $this->assign('clist', $clist);
            
        $this->assign('list', $list);
        $this->display();
    }

    public function brandAdm() {
        if (IS_POST) {
            $data = I('post.');
            if (empty($data['id'])) {
                $ret = M('brand')->add($data);
            } else {
                $ret = M('brand')->where(['id' => $data['id']])->save($data);
            }
            if ($ret !== false) {
                $this->ajaxReturn(1, "操作成功");
            } else {
                $this->ajaxReturn(0, "操作失败");
            }
        } else {
            $id = I('id');
            $info = M('brand')->where(['id' => $id])->find();
            $clist=M('goods_category')->where()->getField("id,name");
            $this->assign('clist', $clist);
            $this->assign('id', $id);
            $this->assign('info', $info);
            SL($info);
            $this->ajaxReturn(1, 'success', $this->fetch());
        }
    }

    /**
     * 删除   
     */
    public function brandDel() {
        $id = array_unique((array) I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));
        if (M('brand')->where($map)->delete()) {
            session('ADMIN_MENU_LIST', null);
            //记录行为
            action_log('delete brand', 'brand', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

  public function catelist() {
              $title = I('title');
        if (!empty($title)) {
            $map['name'] = ['like', "%$title%"];
        }
        $sortfld = I('sortfld', "id", 'trim');
        $sortord = I('sortord', "desc", 'trim');
        $list = $this->lists('goods_category', $map, "`$sortfld` $sortord");
        $this->assign('list', $list);
        $this->display();
    }
    
    public function cateAdm() {
        if (IS_POST) {
            $data = I('post.');
            if (empty($data['id'])) {
                $ret = M('goods_category')->add($data);
            } else {
                $ret = M('goods_category')->where(['id' => $data['id']])->save($data);
            }
            if ($ret !== false) {
                $this->ajaxReturn(1, "操作成功");
            } else {
                $this->ajaxReturn(0, "操作失败");
            }
        } else {
            $id = I('id');
            $info = M('goods_category')->where(['id' => $id])->find();
            $this->assign('id', $id);
            $this->assign('info', $info);

            $this->ajaxReturn(1, 'success', $this->fetch());
        }
    }

 
    /**
     * 删除   
     */
    public function cateDel() {
        $id = array_unique((array) I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));
        if (M('goods_category')->where($map)->delete()) {
            session('ADMIN_MENU_LIST', null);
            //记录行为
            action_log('delete good_cate', 'good_cate', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }
  public function ajax_edit_cate(){
    
      parent::ajax_edit("goods_category");
  }
 
    public function ajax_edit_brand(){
    
      parent::ajax_edit("brand");
  }
  
}
 
